package com.example.redisdemo.controller;

import com.example.redisdemo.model.RedisInfo;
import com.example.redisdemo.model.Student;
import com.example.redisdemo.redis.RedisJava;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;

@Controller
public class RedisController {
    RedisInfo ri = new RedisInfo();

    @RequestMapping(value = "/querystudent", method = RequestMethod.GET)
    public Student queryStudentBySno(String sno) {
        Jedis jedis = new Jedis(ri.getRIp(),ri.getRPort());
        jedis.set("name","modige");
        Student s = new Student();
        s.setSname(jedis.get("name"));
        return s;
    }

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ModelAndView allStudentBySno(String sno) {
        Jedis jedis = new Jedis(ri.getRIp(),ri.getRPort());
        jedis.set("name","modige");
        Student s = new Student();
        s.setSname(jedis.get("name"));
        ModelAndView mvc = new ModelAndView("student");
        mvc.addObject(s);
        return mvc;
    }

    @PostMapping(value = "addStudent")
    public ModelAndView addStudent(@RequestParam("sname") String sname,@RequestParam("sno") String sno) {
        Jedis jedis = new Jedis(ri.getRIp(),ri.getRPort());
        jedis.set(sname,sno);
        Student s = new Student();
        s.setSname(sname);
        s.setSage(sname);
        s.setSname(jedis.get("name"));
        ModelAndView mvc = new ModelAndView("student");
        mvc.addObject(s);
        return mvc;
    }


}
